import api from '@/api';
import { CategrayType, GameItem, SystemSetting, UserInfo } from '@/typings';
import { dayjs } from '@/utils';
import storage from '@/utils/storage';
import { defineStore } from 'pinia';
import { ref } from 'vue';

const getSystemSetting = () => {
  return storage.getItem('systemSetting') || {};
};

const setSystemSetting = (settings) => {
  storage.setItem('systemSetting', settings);
};

export const useStore = defineStore('root', () => {
  const systemSetting = ref<SystemSetting>({}); // 系统配置
  const userInfo = ref<UserInfo>({}); // 用户信息
  const gameList = ref<GameItem[]>([]);
  const movieList = ref<GameItem[]>([]);
  const teleplayList = ref<GameItem[]>([]);

  function clearStore() {
    systemSetting.value = {};
    userInfo.value = {};
  }

  function getUserInfo() {
    let userName = storage.getItem('userName');
    if (!userName) {
      userName = 'default';
      storage.setItem('userName', userName);
      storage.setItem(userName, {
        userName,
        createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      });
    }
    const info = storage.getItem(userName);
    info.game = new Set(info.game || []);
    info.movie = new Set(info.movie || []);
    info.teleplay = new Set(info.teleplay || []);
    userInfo.value = info as UserInfo;
  }

  function updateUserInfo() {
    const info = { ...userInfo.value } as any;
    info.game = Array.from(info.game);
    info.movie = Array.from(info.movie);
    info.teleplay = Array.from(info.teleplay);
    storage.setItem(info.userName, info);
  }

  async function getGameList() {
    const res = await api.getGameList();
    gameList.value = res.map((i) => {
      return { ...i, cover: `/game/${i.index}.jpg`, collect: userInfo.value.game?.has(i.index) };
    });
  }

  async function getMovieList() {
    const res = await api.getGameList();
    movieList.value = res.map((i) => {
      return { ...i, cover: `/game/${i.index}.jpg`, collect: userInfo.value.movie?.has(i.index) };
    });
  }

  async function getTeleplayList() {
    const res = await api.getGameList();
    teleplayList.value = res.map((i) => {
      return {
        ...i,
        cover: `/game/${i.index}.jpg`,
        collect: userInfo.value.teleplay?.has(i.index),
      };
    });
  }

  function initData() {
    getUserInfo();
    getGameList();
    getMovieList();
    getTeleplayList();
  }

  function handleChangeCollect(list, infoSet, index: string, flag: boolean) {
    for (let i = 0; i < list.length; i++) {
      const item = list[i];
      if (item.index === index) {
        item.collect = flag;
        if (flag) {
          infoSet.add(index);
        } else {
          infoSet.delete(index);
        }
        break;
      }
    }
  }

  function changeCollect(type: CategrayType, index: string, flag: boolean) {
    if (type === 'game') {
      handleChangeCollect(gameList.value, userInfo.value.game, index, flag);
    } else if (type === 'movie') {
      handleChangeCollect(gameList.value, userInfo.value.game, index, flag);
    } else if (type === 'teleplay') {
      handleChangeCollect(gameList.value, userInfo.value.game, index, flag);
    }
    updateUserInfo();
  }

  return {
    systemSetting,
    userInfo,
    gameList,
    movieList,
    teleplayList,
    clearStore,
    initData,
    changeCollect,
  };
});
